package setting

import (
	"context"
	"encoding/json"

	"github.com/blog-zero/api/admin/internal/common/errorx"
	"github.com/blog-zero/api/admin/internal/svc"
	"github.com/blog-zero/api/admin/internal/types"
	"github.com/blog-zero/rpc/oms/omsclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderSettingListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewOrderSettingListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderSettingListLogic {
	return &OrderSettingListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *OrderSettingListLogic) OrderSettingList(req *types.ListOrderSettingReq) (*types.ListOrderSettingResp, error) {
	resp, err := l.svcCtx.OrderSettingService.OrderSettingList(l.ctx, &omsclient.OrderSettingListReq{
		Current:  req.Current,
		PageSize: req.PageSize,
	})

	if err != nil {
		data, _ := json.Marshal(req)
		logx.WithContext(l.ctx).Errorf("参数: %s,查询订单设置列表异常:%s", string(data), err.Error())
		return nil, errorx.NewDefaultError("查询订单设置失败")
	}

	var list []*types.ListtOrderSettingData

	for _, item := range resp.List {
		list = append(list, &types.ListtOrderSettingData{
			Id:                  item.Id,
			FlashOrderOvertime:  item.FlashOrderOvertime,
			NormalOrderOvertime: item.NormalOrderOvertime,
			ConfirmOvertime:     item.ConfirmOvertime,
			FinishOvertime:      item.FinishOvertime,
			CommentOvertime:     item.CommentOvertime,
		})
	}

	return &types.ListOrderSettingResp{
		Code:    "000000",
		Message: "查询用户列表成功",
		Data: &types.ListOrderSettingRespData{
			Current:  req.Current,
			PageSize: req.PageSize,
			Total:    resp.Total,
			Data:     list,
		},
		Success: true,
	}, nil
}
